package com.flight.trans.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.LeavePortGroupBoardSortAdapter;
import com.flight.trans.bean.LeavePortGroupBoardDetailBean.GroupBoardDetailData.DetailsOrderData;
import com.flight.trans.bean.TransportSortBean;
import com.flight.trans.global.Constant;
import com.flight.trans.base.BaseDialog;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.DensityUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : YanKing
 * @date : 2021/12/25  9:53
 * @Description :
 */
public class BoardSortDialog extends BaseDialog {
    private LinearLayout mLayoutBg;
    private RecyclerView mRvContainer;
    private Button mBtnCancel,//取消按钮
            mBtnConfirm;//确定按钮

    private LeavePortGroupBoardSortAdapter sortAdapter;
    private List<DetailsOrderData> mGroupBoardBeanList ;

    public static BoardSortDialog getInstance(Context mContext) {
        return new BoardSortDialog(mContext);
    }

    private BoardSortDialog(Context mContext) {
        this.mContext = mContext;
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
        builder();
    }

    private BoardSortDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_board_sort, null);

        // 获取自定义Dialog布局中的控件
        mLayoutBg = view.findViewById(R.id.mLayoutBg);
        mRvContainer = view.findViewById(R.id.mRvContainer);
        mBtnCancel = view.findViewById(R.id.mBtnCancel);
        mBtnConfirm = view.findViewById(R.id.mBtnConfirm);

        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);

        // 调整dialog背景大小
        mLayoutBg.setLayoutParams(new FrameLayout.LayoutParams( 4 * DensityUtils.getScreenWidth((Activity) mContext) / 5, 4 * DensityUtils.getScreenHeight((Activity) mContext) / 5));

        //初始化取消按钮
        setCancelButton();
        //点击空白处可消失
        setCancelable(false);
        return this;
    }

    /**
     * 设置列表
     * @param groupBoardBeanList
     * @return
     */
    public BoardSortDialog setGroupBoardList(List<DetailsOrderData> groupBoardBeanList){
        this.mGroupBoardBeanList = groupBoardBeanList ;

        initGroupBoardAdapter();
        return this ;
    }

    private void initGroupBoardAdapter(){
        if(sortAdapter == null) {
            sortAdapter = new LeavePortGroupBoardSortAdapter(mContext, mGroupBoardBeanList);
            mRvContainer.setAdapter(sortAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mContext));

            sortAdapter.setOnItemClickListener(new SortItemClickListener());
        }else {
            sortAdapter.updateAdapter(mGroupBoardBeanList);
        }
    }

    /**
     * 条目点击事件
     */
    private class SortItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            int count = Constant.SortNullPosition;
            if(mGroupBoardBeanList.get(position).getPosition() == Constant.SortNullPosition) {
                count = 1;
                for (int i = 0; i < mGroupBoardBeanList.size(); i++) {
                    if (mGroupBoardBeanList.get(i).getPosition() != Constant.SortNullPosition)
                        count++;
                }
            }else {
                for (int i = 0; i < mGroupBoardBeanList.size(); i++) {
                    if (mGroupBoardBeanList.get(i).getPosition() > mGroupBoardBeanList.get(position).getPosition() && mGroupBoardBeanList.get(i).getPosition() < Constant.SortNullPosition)
                        mGroupBoardBeanList.get(i).setPosition(mGroupBoardBeanList.get(i).getPosition() - 1);
                }
            }

            mGroupBoardBeanList.get(position).setPosition(count);
            initGroupBoardAdapter();
        }
    }

    /**
     * 设置是否点击空白处消失,false为不消失
     *
     * @param cancel
     * @return
     */
    public BoardSortDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    /**
     * 设置确定按钮
     *
     * @param listener
     * @return
     */
    public BoardSortDialog setConfirmButton(final OnConfirmClickListener listener) {
        mBtnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                listener.onClick(listSorting(mGroupBoardBeanList));
            }
        });
        return this;
    }


    /**
     * 集合排序
     * @param listInAppxList
     * @return
     */
    public static List<TransportSortBean> listSorting(List<DetailsOrderData> listInAppxList) {
        Comparator<DetailsOrderData> comparator = new Comparator<DetailsOrderData>() {
            @Override
            public int compare(DetailsOrderData groupBoardStart, DetailsOrderData groupBoardEnd) {
                if(groupBoardStart.getPosition() > groupBoardEnd.getPosition())
                    return 1;
                else {
                    return -1;
                }
            }
        };
        //这里就会自动根据规则进行排序
        Collections.sort(listInAppxList, comparator);
        List<TransportSortBean> list = new ArrayList<>();

        for (int i = 0; i < listInAppxList.size(); i++) {
            list.add(new TransportSortBean(listInAppxList.get(i).getTransportCode() , listInAppxList.get(i).getVolumeSum()));
        }

        return list;
    }

    /**
     * 设置取消按钮
     *
     * @return
     */
    public BoardSortDialog setCancelButton() {
        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        return this;
    }

    /**
     * 设置确定按钮
     *
     * @param listener
     * @return
     */
    public BoardSortDialog setCancelButton(final OnClickListener listener) {
        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                listener.onClick(v);
            }
        });
        return this;
    }

    public interface OnConfirmClickListener {
        void onClick(List<TransportSortBean> sortList);
    }
}
